package top.wenzz.manage.system.manage.entity;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotBlank;
import top.wenzz.commons.annotation.Edit;
import top.wenzz.commons.entity.DataEntity;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by cui on 2017/7/7.
 */
@Entity( name = "sys_permission" )
@Data
public class SysPermission extends DataEntity<SysPermission> {
    @Column(length = 50, nullable = false)
    @Length(min = 2, max = 50, message = "菜单文本长度为2-50")
    @NotBlank(message = "菜单文本不能为空")
    @Edit("菜单文本")
    private String label;

    @Column(length = 50)
    @Length(max = 50, message = "菜单图标最大长度为50")
    @Edit("菜单图标")
    private String icon;

    @Length(max = 255, message = "uri最大长度为255")
    @Edit("uri")
    private String path;

    @Column(length = 100)
    @Length(max = 100, message = "权限标识最大长度为50")
    @Edit("权限标识")
    private String sn;

    @Edit("排序号")
    private int weight;

    @Edit("父节点")
    private int pid;

    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    @Transient
    private List<SysPermission> children = new ArrayList<>();

    @ManyToMany
    @JoinTable(name = "sys_permission_resources", joinColumns = {@JoinColumn(name = "permission_id")}, inverseJoinColumns = {@JoinColumn(name = "resources_id")})
    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    private List<SysResources> resources = new ArrayList<>();
}
